<?php

class DefaultController extends Controller
{
	public function init(){
		//die('xxx');
		if(Yii::app()->user->isGuest){
			//Yii::app()->request->redirect('admin/default/login',true);
		}
	}
	public function actionIndex()
	{
		$this->render('index');
	}
	public function actionLogin(){
		//Load user model
		$this->layout = '//layouts/login';
		//print_r($this);
		Yii::app()->getModule('user');
		$model=new User;//We can set scenario like $model = new User('login')
		$model->setScenario('login');
		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
		if(isset($_POST)){
			//print_r($_POST);
			//print_r($model->attributes);
			//print_r($model);
			//die();
		}
		// collect user input data
		if(isset($_POST['User']))
		{
			$model->attributes=$_POST['User'];
			// validate user input and redirect to the previous page if valid
			if($model->validate('login') && $model->login()){
				$this->redirect(Yii::app()->user->returnUrl);
			}
			
		}
		// display the login form
		$this->render('login',array('model'=>$model));
	}
	/**
	 * Logs out the current user and redirect to homepage.
	 */
	public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}
}